import React, { Component } from 'react'
import {Redirect,Route} from "react-router-dom"

export default class RouterAuth extends Component {
  render() {
    // (1)获取到外部数据
    const {routerConfig,location} = this.props
    const {pathname} = location
    const token = localStorage.getItem("token")
    // (2)判断用户的访问路径是否在配置文件中
    const targetRouterConfig = routerConfig.find((item)=>{
      return item.path.replace(/\s*/g,"") === pathname
    })
    // (3)判断匹配的路径是否是我们配置的路径
    if(targetRouterConfig){
      const {component} = targetRouterConfig
      if(targetRouterConfig.auth){
        if(token){
          return <Route exact path={pathname} component={component}/>
        }else{
          return <Redirect to="/login"/>
        }
      }else{
        return <Route exact path={pathname} component={component}/>
      }
    }else{
      return <Redirect to="/404"/>
    }
  }
}


